
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2024/10/25 10:58
# @Author  : LYHang00
# @FileName: upload_file.py
# @Software: PyCharm
import os
import sys
import time
import click
import logging
from pathlib import Path


BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(BASE_DIR)
sys.path.append(os.path.join(BASE_DIR, "utils"))
PARENT_DIR = os.path.dirname(BASE_DIR)

logging.basicConfig(level=logging.INFO,
                    format="%(asctime)s  %(levelname)s:%(message)s")
from utils.common import func_retry
from utils.obs_helper import ObsHelper

CONAN_OBS_BUCKET_NAME = "openubmc-dailybuild"


class PipeLineLogHelper:

    def __init__(self, ak, sk, obs_endpoint, obs_bucket_name):
        self.obs_helper = ObsHelper(ak, sk, obs_endpoint)
        self.obs_bucket_name = obs_bucket_name
        self.date = time.localtime(time.time())

    @func_retry()
    def upload_obs(self, full_path):
        self.obs_helper.upload_file(self.obs_bucket_name, "conan_cache/"+full_path.name, full_path)


@click.command()
@click.option("--ak")
@click.option("--sk")
@click.option("--obs_endpoint")
def main(ak, sk, obs_endpoint):
    logging.info("=" * 20 + "start to upload the log to obs" + "=" * 20)

    pipeline_log_helper = PipeLineLogHelper(ak, sk, obs_endpoint, CONAN_OBS_BUCKET_NAME)
    full_path = Path(os.path.join("/root", "conan_cache.tar.gz"))

    if full_path.is_file():
        pipeline_log_helper.upload_obs(full_path)
    else:
        logging.info("The file:{} is not exists.".format(full_path))


if __name__ == '__main__':
    main()